package com.example.app_info_mng.controller.devloper;
import com.example.app_info_mng.pojo.DevUser;
import com.example.app_info_mng.service.DevUserService;
import com.example.app_info_mng.utils.Constants;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value="/dev")
public class DevUserController {
	private Logger logger = Logger.getLogger(DevUserController.class);

	@Resource
	private DevUserService devUserService;

	@GetMapping(value="/login")
	public String login(){
		logger.debug("LoginController welcome AppInfoSystem develpor==================");
		return "devlogin";
	}
	//http:localhost:8080/appInfo/dev/login

	@PostMapping("/login")
	public String doLogin(@RequestParam String devCode,@RequestParam String devPassword,HttpServletRequest request,HttpSession session){
		logger.debug("doLogin====================================");
		//调用service方法，进行用户匹配
		DevUser user = null;
		try {
//			调用业务类，进行用户名和密码的校验
			user = devUserService.login(devCode,devPassword);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		处理结果
		if(null != user){//登录成功
			//放入session
			request.getSession().setAttribute(Constants.DEV_USER_SESSION, user);
			//页面跳转（main.jsp）
			return "redirect:/dev/flatform/main";
		}else{
			//页面跳转（login.jsp）带出提示信息--转发
			request.setAttribute("error", "用户名或密码不正确");
			return "devlogin";
		}
	}
	@GetMapping("flatform/main")
	public String toDevIndex(){
		return "developer/main";
//		修改首页
	}
}

